home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_108 / bash-108.zoo / src / make_cmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-22  |  15.1 KB  |  618 lines

  1. /* make_command.c --
  2.    Functions for making instances of the various parser constructs. */
  3.  
  4. /* Copyright (C) 1989 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7.  
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 1, or (at your option) any later
  11. version.
  12.  
  13. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License along
  19. with Bash; see the file COPYING.  If not, write to the Free Software
  20. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  21.  
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <fcntl.h>
  25. #include <sys/file.h>
  26.  
  27. extern int errno;
  28. extern char *strerror ();
  29.  
  30. #if defined (HAVE_VPRINTF)
  31. #include <varargs.h>
  32. #endif
  33.  
  34. #include "shell.h"
  35. #include "flags.h"
  36.  
  37. #if defined (JOB_CONTROL)
  38. #include "jobs.h"
  39. #endif
  40.  
  41. WORD_DESC *
  42. make_word (string)
  43.      char *string;
  44. {
  45.   WORD_DESC *temp = (WORD_DESC *)xmalloc (sizeof (WORD_DESC));
  46.   temp->word = savestring (string);
  47.   temp->quoted = temp->dollar_present = temp->assignment = 0;
  48.   while (*string) {
  49.     if (*string == '$') temp->dollar_present = 1;
  50.  
  51.     if (member (*string, "'`\\\""))
  52.       {
  53.     temp->quoted = 1;
  54.     if (*string == '\\')
  55.       string++;
  56.       }
  57.     if (*string)
  58.       (string++);
  59.   }
  60.   return (temp);
  61. }
  62.  
  63. WORD_DESC *
  64. make_word_from_token (token)
  65.      int token;
  66. {
  67.   char tokenizer[2];
  68.  
  69.   tokenizer[0] = token;
  70.   tokenizer[1] = '\0';
  71.  
  72.   return (make_word (tokenizer));
  73. }
  74.  
  75. WORD_LIST *
  76. make_word_list (word, link)
  77.      WORD_DESC *word;
  78.      WORD_LIST *link;
  79. {
  80.   WORD_LIST *temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
  81.   temp->word = word;
  82.   temp->next = link;
  83.   return (temp);
  84. }
  85.  
  86. WORD_LIST *
  87. add_string_to_list (string, list)
  88.      char *string;
  89.      WORD_LIST *list;
  90. {
  91.   WORD_LIST *temp = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
  92.   temp->word = make_word (string);
  93.   temp->next = list;
  94.   return (temp);
  95. }
  96.  
  97. WORD_DESC *
  98. coerce_to_word (number)
  99.      int number;
  100. {
  101.   char *string = (char *)alloca (24);
  102.   sprintf (string, "%d", number);
  103.   return (make_word (string));
  104. }
  105.  
  106. COMMAND *
  107. make_command (type, pointer)
  108.      enum command_type type;
  109.      SIMPLE_COM *pointer;
  110. {
  111.   COMMAND *temp = (COMMAND *)xmalloc (sizeof (COMMAND));
  112.   temp->type = type;
  113.   temp->value.Simple = pointer;
  114.   temp->subshell = temp->invert_pipeline = 0;
  115.   temp->redirects = (REDIRECT *)NULL;
  116.   return (temp);
  117. }
  118.  
  119. COMMAND *
  120. command_connect (com1, com2, connector)
  121.      COMMAND *com1, *com2;
  122.      int connector;
  123. {
  124.   CONNECTION *temp = (CONNECTION *)xmalloc (sizeof (CONNECTION));
  125.   temp->connector = connector;
  126.   temp->first = com1;
  127.   temp->second = com2;
  128.   return (make_command (cm_connection, (SIMPLE_COM *)temp));
  129. }
  130.  
  131. COMMAND *
  132. make_for_command (name, map_list, action)
  133.      WORD_DESC *name;
  134.      WORD_LIST *map_list;
  135.      COMMAND *action;
  136. {
  137.   FOR_COM *temp = (FOR_COM *)xmalloc (sizeof (FOR_COM));
  138.  
  139.   temp->name = name;
  140.   temp->map_list = map_list;
  141.   temp->action = action;
  142.   return (make_command (cm_for, (SIMPLE_COM *)temp));
  143. }
  144.  
  145. COMMAND *
  146. make_group_command (command)
  147.      COMMAND *command;
  148. {
  149.   GROUP_COM *temp = (GROUP_COM *)xmalloc (sizeof (GROUP_COM));
  150.  
  151.   temp->command = command;
  152.   return (make_command (cm_group, (SIMPLE_COM *)temp));
  153. }
  154.  
  155. COMMAND *
  156. make_case_command (word, clauses)
  157.      WORD_DESC *word;
  158.      PATTERN_LIST *clauses;
  159. {
  160.   WORD_LIST *reverse_list ();
  161.   CASE_COM *temp = (CASE_COM *)xmalloc (sizeof (CASE_COM));
  162.   temp->word = word;
  163.   temp->clauses = (PATTERN_LIST *)reverse_list (clauses);
  164.   return (make_command (cm_case, (SIMPLE_COM *)temp));
  165. }
  166.  
  167. PATTERN_LIST *
  168. make_pattern_list (patterns, action)
  169.      WORD_LIST *patterns;
  170.      COMMAND *action;
  171. {
  172.   PATTERN_LIST *temp = (PATTERN_LIST *)xmalloc (sizeof (PATTERN_LIST));
  173.   temp->patterns = patterns;
  174.   temp->action = action;
  175.   temp->next = NULL;
  176.   return (temp);
  177. }
  178.  
  179. COMMAND *
  180. make_if_command (test, true_case, false_case)
  181.      COMMAND *test, *true_case, *false_case;
  182. {
  183.   IF_COM *temp = (IF_COM *)xmalloc (sizeof (IF_COM));
  184.   temp->test = test;
  185.   temp->true_case = true_case;
  186.   temp->false_case = false_case;
  187.   return (make_command (cm_if, (SIMPLE_COM *)temp));
  188. }
  189.  
  190. COMMAND *
  191. make_until_or_while (test, action, which)
  192.      COMMAND *test, *action;
  193.      enum command_type which;
  194. {
  195.   WHILE_COM *temp = (WHILE_COM *)xmalloc (sizeof (WHILE_COM));
  196.   temp->test = test;
  197.   temp->action = action;
  198.   return (make_command (which, (SIMPLE_COM *)temp));
  199. }
  200.  
  201. COMMAND *
  202. make_while_command (test, action)
  203.      COMMAND *test, *action;
  204. {
  205.   return (make_until_or_while (test, action, cm_while));
  206. }
  207.  
  208. COMMAND *
  209. make_until_command (test, action)
  210.      COMMAND *test, *action;
  211. {
  212.   return (make_until_or_while (test, action, cm_until));
  213. }
  214.  
  215. COMMAND *
  216. make_bare_simple_command ()
  217. {
  218.   COMMAND *command;
  219.   SIMPLE_COM *temp = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
  220.  
  221.   temp->words = (WORD_LIST *)NULL;
  222.   temp->redirects = (REDIRECT *)NULL;
  223.   command = (COMMAND *)xmalloc (sizeof (COMMAND));
  224.   command->type = cm_simple;
  225.   command->redirects = (REDIRECT *)NULL;
  226.   command->subshell = command->invert_pipeline = 0;
  227.   command->value.Simple = temp;
  228.   return (command);
  229. }
  230.  
  231. /* Return a command which is the connection of the word or redirection
  232.    in ELEMENT, and the command * or NULL in COMMAND. */
  233. COMMAND *
  234. make_simple_command (element, command)
  235.      ELEMENT element;
  236.      COMMAND *command;
  237. {
  238.   /* If we are starting from scratch, then make the initial command
  239.      structure.  Also note that we have to fill in all the slots, since
  240.      malloc doesn't return zeroed space. */
  241.   if (!command)
  242.     {
  243.       SIMPLE_COM *temp = (SIMPLE_COM *)xmalloc (sizeof (SIMPLE_COM));
  244.       temp->words = (WORD_LIST *)NULL;
  245.       temp->redirects = (REDIRECT *)NULL;
  246.       command = (COMMAND *)xmalloc (sizeof (COMMAND));
  247.       command->type = cm_simple;
  248.       command->redirects = (REDIRECT *)NULL;
  249.       command->subshell = command->invert_pipeline = 0;
  250.       command->value.Simple = temp;
  251.     }
  252.   if (element.word)
  253.     {
  254.       WORD_LIST *tw = (WORD_LIST *)xmalloc (sizeof (WORD_LIST));
  255.       tw->word = element.word;
  256.       tw->next = command->value.Simple->words;
  257.       command->value.Simple->words = tw;
  258.     }
  259.   else
  260.     {
  261.       REDIRECT *r = element.redirect;
  262.       /* Due to the way <> is implemented, there may be more than a single
  263.          redirection in element.redirect.  We just follow the chain as far
  264.          as it goes, and hook onto the end. */
  265.       while (r->next)
  266.     r = r->next;
  267.       r->next = command->value.Simple->redirects;
  268.       command->value.Simple->redirects = element.redirect;
  269.     }
  270.   return (command);
  271. }
  272.  
  273. make_here_document (temp)
  274.      REDIRECT *temp;
  275. {
  276.   int kill_leading = 0;
  277.  
  278.   switch (temp->instruction)
  279.     {
  280.       /* Because we are Bourne compatible, we read the input for this
  281.          << or <<- redirection now, from wherever input is coming from.
  282.          We store the input read into a WORD_DESC.  Replace the text of
  283.          the redirectee.word with the new input text.  If <<- is on,
  284.          then remove leading whitespace from each line. */
  285.  
  286.       case r_deblank_reading_until:    /* <<-foo */
  287.     kill_leading++;
  288.     /* ... */
  289.       case r_reading_until:        /* <<foo */
  290.     {
  291.       char *redirection_expand (), *redirectee_word;
  292.       int len;
  293.  
  294.       char *document = (char *)NULL;
  295.       int document_index = 0, document_size = 0;
  296.  
  297.       /* Because of Bourne shell semantics, we turn off globbing, but
  298.          only for this style of redirection.  I feel a little ill.  */
  299.       {
  300.         extern int disallow_filename_globbing;
  301.         int old_value = disallow_filename_globbing;
  302.         disallow_filename_globbing = 1;
  303.  
  304.         redirectee_word = redirection_expand (temp->redirectee.filename);
  305.  
  306.         disallow_filename_globbing = old_value;
  307.       }
  308.  
  309.       /* redirection_expand will return NULL if the expansion results in
  310.          multiple words or no words.  Check for that here, and just abort
  311.          this here document if it does. */
  312.       if (redirectee_word)
  313.         len = strlen (redirectee_word);
  314.       else
  315.         {
  316.           temp->here_doc_eof = savestring ("");
  317.           goto document_done;
  318.         }
  319.  
  320.       free (temp->redirectee.filename->word);
  321.       temp->here_doc_eof = redirectee_word;
  322.  
  323.       /* Read lines from wherever lines are coming from.
  324.          For each line read, if kill_leading, then kill the
  325.          leading whitespace.
  326.          If the line matches redirectee_word exactly, then we have
  327.          manufactured the document.  Otherwise, add the line to the
  328.          list of lines in the document. */
  329.       {
  330.         extern char *read_secondary_line ();
  331.         char *line;
  332.         int l;
  333.  
  334.         while (line = read_secondary_line ())
  335.           {
  336.             if (!line)
  337.           goto document_done;
  338.  
  339.             if (kill_leading)
  340.           {
  341.             register int i;
  342.  
  343.             /* Hack:  To be compatible with some Bourne shells, we 
  344.                check the word before stripping the whitespace.  This
  345.                is a hack, though. */
  346.             if ((strncmp (line, redirectee_word, len) == 0) &&
  347.                 line[len] == '\n')
  348.               goto document_done;
  349.  
  350.             for (i = 0; whitespace (line[i]); i++)
  351.               ;
  352.  
  353.             if (i)
  354.               strcpy (&line[0], &line[i]);
  355.           }
  356.  
  357.             if ((strncmp (line, redirectee_word, len) == 0) && line[len] == '\n')
  358.           goto document_done;
  359.  
  360.             l = strlen (line);
  361.             if (l + document_index >= document_size)
  362.           {
  363.             document = (char *)
  364.               xrealloc (document, (document_size += (10 * l)));
  365.           }
  366.  
  367.         if (l != 0)
  368.           {
  369.             strcpy (&document[document_index], line);
  370.             free (line);
  371.             document_index += l;
  372.           }
  373.           }
  374.   document_done:
  375.         if (!document)
  376.           document = savestring ("");
  377.         temp->redirectee.filename->word = document;
  378.       }
  379.     }
  380.     }
  381. }
  382.    
  383. /* Generate a REDIRECT from SOURCE, DEST, and INSTRUCTION. 
  384.    INSTRUCTION is the instruction type, SOURCE is an INT,
  385.    and DEST is an INT or a WORD_DESC *. */
  386. REDIRECT *
  387. make_redirection (source, instruction, dest)
  388.      enum r_instruction instruction;
  389. {
  390.   REDIRECT *temp = (REDIRECT *)xmalloc (sizeof (REDIRECT));
  391.  
  392.   /* First do the common cases. */
  393.   temp->redirector = source;
  394.   temp->redirectee.dest = dest;
  395.   temp->instruction = instruction;
  396.   temp->next = (REDIRECT *)NULL;
  397.  
  398.   switch (instruction) {
  399.  
  400.   case r_output_direction:    /* >foo */
  401.   case r_output_force:        /* >| foo */
  402.     temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
  403.     break;
  404.  
  405.   case r_input_direction:    /* <foo */
  406.   case r_inputa_direction:    /* foo & makes this. */
  407.     temp->flags = O_RDONLY;
  408.     break;
  409.  
  410.   case r_appending_to:        /* >>foo */
  411.     temp->flags = O_APPEND | O_WRONLY | O_CREAT;
  412.     break;
  413.  
  414.   case r_deblank_reading_until:    /* <<-foo */
  415.   case r_reading_until:        /* << foo */
  416.     break;
  417.  
  418.   case r_duplicating:        /* 1<&2 */
  419.   case r_close_this:        /* <&- */
  420.     break;
  421.     
  422.   case r_err_and_out:        /* command &>filename */
  423.     temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
  424.     break;
  425.  
  426.   case r_input_output:
  427.     temp->flags = O_RDWR;
  428.     break;
  429.  
  430.   default:
  431.     report_error ("Redirection instruction from yyparse () '%d' is\n\
  432. out of range in make_redirection ().", instruction);
  433.     abort ();
  434.     break;
  435.   }
  436.   return (temp);
  437. }
  438.  
  439. COMMAND *
  440. make_function_def (name, command)
  441.      WORD_DESC *name;
  442.      COMMAND *command;
  443. {
  444.   FUNCTION_DEF *temp = (FUNCTION_DEF *)xmalloc (sizeof (FUNCTION_DEF));
  445.   temp->command = command;
  446.   temp->name = name;
  447.   return (make_command (cm_function_def, (SIMPLE_COM *)temp));
  448. }
  449.  
  450. /* Reverse the word list and redirection list in the simple command
  451.    has just been parsed.  It seems simpler to do this here the one
  452.    time then by any other method that I can think of. */
  453. COMMAND *
  454. clean_simple_command (command)
  455.      COMMAND *command;
  456. {
  457.   if (command->type != cm_simple)
  458.     programming_error ("clean_simple_command () got a command with type %d.", command->type);
  459.   else {
  460.     command->value.Simple->words =
  461.       (WORD_LIST *)reverse_list (command->value.Simple->words);
  462.     command->value.Simple->redirects = 
  463.       (REDIRECT *)reverse_list (command->value.Simple->redirects);
  464.   }
  465.   return (command);
  466. }
  467.  
  468. /* Cons up a new array of words.  The words are taken from LIST,
  469.    which is a WORD_LIST *.  Absolutely everything is malloc'ed,
  470.    so you should free everything in this array when you are done.
  471.    The array is NULL terminated. */
  472. char **
  473. make_word_array (list)
  474.      WORD_LIST *list;
  475. {
  476.   int count = list_length (list);
  477.   char **array = (char **)xmalloc ((1 + count) * sizeof (char *));
  478.  
  479.   for (count = 0; list; count++)
  480.     {
  481.       array[count] = (char *)xmalloc (1 + strlen (list->word->word));
  482.       strcpy (array[count], list->word->word);
  483.       list = list->next;
  484.     }
  485.   array[count] = (char *)NULL;
  486.   return (array);
  487. }
  488.  
  489. /* Report an error having to do with FILENAME. */
  490. file_error (filename)
  491.      char *filename;
  492. {
  493.   report_error ("%s: %s", filename, strerror (errno));
  494. }
  495.  
  496. #if !defined (HAVE_VPRINTF)
  497. programming_error (reason, arg1, arg2, arg3, arg4, arg5)
  498.      char *reason;
  499. {
  500.   extern char *the_current_maintainer;
  501.  
  502. #ifdef JOB_CONTROL
  503.   {
  504.     extern int shell_pgrp;
  505.     give_terminal_to (shell_pgrp);
  506.   }
  507. #endif  /* JOB_CONTROL */
  508.  
  509.   report_error (reason, arg1, arg2);
  510.   fprintf (stderr, "Tell %s to fix this someday.\n", the_current_maintainer);
  511.  
  512. #if defined (MAKE_BUG_REPORTS)
  513.   if (1)
  514.     {
  515.       fprintf (stderr, "Mailing a bug report...");
  516.       fflush (stderr);
  517.       make_bug_report (reason, arg1, arg2, arg3, arg4, arg5);
  518.       fprintf (stderr, "done.\n");
  519.     }
  520. #endif
  521.  
  522.   fprintf (stderr, "Stopping myself...");
  523.   fflush (stderr);
  524.   abort ();
  525. }
  526.  
  527. report_error (format, arg1, arg2, arg3, arg4, arg5)
  528.      char *format;
  529. {
  530. #if defined (NOTDEF)
  531.   extern char *shell_name, *base_pathname ();
  532.  
  533.   fprintf (stderr, "%s: ", base_pathname (shell_name));
  534. #endif /* NOTDEF */
  535.  
  536.   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  537.   fprintf (stderr, "\n");
  538.   if (exit_immediately_on_error)
  539.     exit (EXECUTION_FAILURE);    /* XXX longjmp (top_level, EXITPROG) ? */
  540. }  
  541.  
  542. fatal_error (format, arg1, arg2, arg3, arg4, arg5)
  543.      char *format;
  544. {
  545.   report_error (format, arg1, arg2, arg3, arg4, arg5);
  546.   exit (2);
  547. }
  548.  
  549. #else /* We have VARARGS support, so use it. */
  550.  
  551. programming_error (va_alist)
  552.      va_dcl
  553. {
  554.   extern char *the_current_maintainer;
  555.  
  556. #ifdef JOB_CONTROL
  557.   {
  558.     extern pid_t shell_pgrp;
  559.     give_terminal_to (shell_pgrp);
  560.   }
  561. #endif  /* JOB_CONTROL */
  562.  
  563.   report_error (va_alist);
  564.   fprintf (stderr, "Tell %s to fix this someday.\n", the_current_maintainer);
  565.  
  566. #if defined (MAKE_BUG_REPORTS)
  567.   if (1)
  568.     {
  569.       fprintf (stderr, "Mailing a bug report...");
  570.       fflush (stderr);
  571.       make_bug_report (va_alist);
  572.       fprintf (stderr, "done.\n");
  573.     }
  574. #endif
  575.   fprintf (stderr, "Stopping myself...");
  576.   fflush (stderr);
  577.   abort ();
  578. }
  579.  
  580. report_error (va_alist)
  581.      va_dcl
  582. {
  583.   va_list args;
  584.   char *format;
  585.  
  586. #if defined (NOTDEF)
  587.   extern char *shell_name, *base_pathname ();
  588.  
  589.   fprintf (stderr, "%s: ", base_pathname (shell_name));
  590. #endif /* NOTDEF */
  591.   va_start (args);
  592.   format = va_arg (args, char *);
  593.   vfprintf (stderr, format, args);
  594.   fprintf (stderr, "\n");
  595.  
  596.   va_end (args);
  597.   if (exit_immediately_on_error)
  598.     exit (EXECUTION_FAILURE);
  599. }
  600.  
  601. fatal_error (va_alist)
  602.      va_dcl
  603. {
  604.   va_list args;
  605.   char *format;
  606.   extern char *shell_name, *base_pathname ();
  607.  
  608.   fprintf (stderr, "%s: ", base_pathname (shell_name));
  609.   va_start (args);
  610.   format = va_arg (args, char *);
  611.   vfprintf (stderr, format, args);
  612.   fprintf (stderr, "\n");
  613.  
  614.   va_end (args);
  615.   exit (2);
  616. }
  617. #endif /* HAVE_VPRINTF */
  618.